﻿activity_tournament = {
	is_shown = {
		highest_held_title_tier > tier_barony
		is_landed = yes
		trigger_if = {
			limit = {
				is_ai = yes
			}
			is_at_war = no
			ai_has_conqueror_personality = no
			NOT = { government_has_flag = government_is_tribal }	# Shogunate
		}
		has_dlc_feature = tours_and_tournaments
		has_dlc_feature = advanced_activities
		#Every other activity must be blocked until an uncrowned AI crowns themselves
		trigger_if = {
			limit = {
				is_ai = yes
			}
			NOT = {
				has_realm_law = uncrowned
			}
		}
	}
	
	can_start_showing_failures_only = {
		NOT = { is_activity_type_on_cooldown = activity_tournament }
		is_available_adult = yes
	}

	is_valid = {
		# If no one shows up
		trigger_if = {
			limit = {
				is_current_phase_active = yes
			}
			has_attending_activity_guests = yes
		}
	}

	on_invalidated = {
		every_attending_character = {
			# Remove tournament only effects
			clean_up_recurring_characters_effect = yes
			tournament_clear_tournament_variables_effect = yes
			tournament_clear_tournament_modifiers_effect = yes
		}
		# Host becomes unlanded
		if = {
			limit = {
				scope:host = { is_landed = no }
			}
			scope:activity = {
				activity_type = { save_scope_as = activity_type }
			}
			every_attending_character = {
				trigger_event = activity_system.0320
			}
		}
		if = { # No one shows up
			limit = {
				has_attending_activity_guests = no
			}
			scope:activity = {
				activity_type = { save_scope_as = activity_type }
				activity_location = { save_scope_as = location }
			}
			scope:host = { trigger_event = activity_system.0100 }
		}
		scope:activity = {
			every_attending_character = {
				limit = { is_ai = no }
				# Remove remaining recurring characters.
				clean_up_recurring_characters_effect = yes
				tournament_clear_tournament_variables_effect = yes
			}
		}
		# Reset the achievement
		if = {
			limit = {
				exists = global_var:started_little_william_marshal_achievement
				exists = global_var:little_william_marshal_achievement_tally
				scope:activity = {
					any_attending_character = {
						is_ai = no
					}
				}
			}
			set_global_variable = {
				name = little_william_marshal_achievement_tally
				value = 0
			}
			set_global_variable = {
				name = started_little_william_marshal_achievement
				value = 0
			}
		}
	}

	on_host_death = {
		# Make the player heir the new host
		if = {
			limit = { exists = scope:host.player_heir }
			scope:host.player_heir = { save_scope_as = new_host }
		}
		# Otherwise pass to any successor
		else = {
			scope:host.primary_heir = { save_scope_as = new_host }
		}
		set_activity_host = scope:new_host
		scope:new_host = {
			trigger_event = { id = activity_system.0080 days = 1 }
		}
	}

	# desc is only shown in debug AI watch window
	ai_will_do = {
		value = 0
		
		# Base
		add = {
			value = 30
			desc = "base"
		}
		
		if = { # Liege already hosting a grand activity
			limit = {
				liege = { has_ongoing_grand_activity_trigger = yes }
			}
			add = {
				value = -1000
				desc = contest_ai_liege_grand_activity
			}
		}
		if = {
			limit = {
				any_activity = {
					is_grand_activity_trigger = yes
					activity_host.top_liege ?= {
						OR = {
							this = root.top_liege # Same realm as root
							any_neighboring_top_liege_realm_owner = { this = root.top_liege } # Neighboring realm
						}
					}
				}
			}
			every_activity = {
				limit = {
					is_grand_activity_trigger = yes
					activity_host.top_liege ?= {
						OR = {
							this = root.top_liege # Same realm as root
							any_neighboring_top_liege_realm_owner = { this = root.top_liege } # Neighboring realm
						}
					}
				}
				add = {
					value = -50
					desc = contest_ai_nearby_grand_activity
				}
			}
		}
		# Personality
		if = { # Avoids responsibilities
			limit = { has_trait = lazy }
			add = {
				value = -20
				desc = lazy
			}
		}
		else_if = { # Better things to do
			limit = { has_trait = diligent }
			add = {
				value = -10
				desc = diligent
			}
		}
		if = { # Likes showing off
			limit = { has_trait = arrogant }
			add = {
				value = 20
				desc = arrogant
			}
		}
		else_if = { #
			limit = { has_trait = humble }
			add = {
				value = -10
				desc = humble
			}
		}
		if = { # In it to win it
			limit = { has_trait = ambitious }
			add = {
				value = 20
				desc = ambitious
			}
		}
		else_if = { #
			limit = { has_trait = content }
			add = {
				value = -10
				desc = content
			}
		}
		if = { # Likes to socialize
			limit = { has_trait = gregarious }
			add = {
				value = 20
				desc = gregarious
			}
		}
		else_if = {
			limit = { has_trait = shy }
			add = {
				value = -10
				desc = shy
			}
		}
		if = {
			limit = { has_trait = reclusive }
			add = {
				value = -25
				desc = reclusive
			}
		}
		# Court
		if = {
			limit = {
				has_royal_court = yes 
				has_court_type = court_warlike
			}
			add = {
				value = 15
				desc = court_warlike
			}
		}
		# Education
		if = {
			limit = { has_trait = education_martial }
			add = {
				value = 25
				desc = education_martial
			}
		}
		if = {
			limit = { has_trait = education_martial_prowess }
			add = {
				value = 25
				desc = education_martial_prowess
			}
		}
		# Culture
		if = {
			limit = { 
				culture = { has_cultural_tradition = tradition_warrior_culture }
			}
			add = {
				value = 25
				desc = tradition_warrior_culture_name
			}
		}
		if = {
			limit = { 
				culture = { has_cultural_tradition = tradition_martial_admiration }
			}
			add = {
				value = 40
				desc = tradition_martial_admiration_name
			}
		}
		if = {
			limit = { 
				culture = { has_cultural_tradition = tradition_chanson_de_geste }
			}
			add = {
				value = 40
				desc = tradition_chanson_de_geste_name
			}
		}
		if = { # Bellicose
			limit = {
				culture = { has_cultural_pillar = ethos_bellicose }
			}
			add = {
				value = 15
				desc = ethos_bellicose
			}
		}
		if = {
			limit = {
				is_independent_ruler = no
				has_vassal_stance = belligerent
			}
			add = {
				value = 10
				desc = belligerent
			}
		}
		# Lifestyle
		if = {
			limit = { has_trait = tourney_participant }
			add = {
				value = 25
				desc = tourney_participant
			}
		}
		if = {
			limit = {
				has_trait = tourney_participant
				OR = {
					has_trait_xp = {
						trait = tourney_participant
						track = foot
						value >= trait_second_level
					}
					has_trait_xp = {
						trait = tourney_participant
						track = horse
						value >= trait_second_level
					}
					has_trait_xp = {
						trait = tourney_participant
						track = bow
						value >= trait_second_level
					}
					has_trait_xp = {
						trait = tourney_participant
						track = wit
						value >= trait_second_level
					}
				}
			}
			add = {
				value = 25
				desc = experienced_tourney_participant
			}
		}
		# Stress
		if = { # Low Stress
            limit = { stress >= lower_than_baseline_stress }
            add = {
            	value = 30
            	desc = "lower_than_baseline_stress"
            }
        }
        if = { # High Stress
        	limit = { stress >= higher_than_baseline_stress }
        	add = {
        		value = 30
        		desc = "higher_than_baseline_stress"
        	}
        }
        if = { # Very High Stress
        	limit = { stress >= high_stress }
        	add = {
        		value = 30
        		desc = "high_stress"
        	}
        }
        # Economic Archetype
		if = { # Too busy booming the economy
            limit = {
                OR = {
                    ai_has_economical_boom_personality = yes
                    has_character_flag = ai_boom
                }
            }
            add = {
            	value = -100
            	desc = "Builder"
            }
        }
        if = {
        	limit = { ai_has_warlike_personality = yes }
        	add = {
            	value = 25
            	desc = "Warlike"
            }
        }
        # Economy Sanity
        if = { # Work first, play later
            limit = {
                exists = capital_county
                capital_county.title_province = { free_building_slots > 0 }
            }
            add = {
            	value = -35
            	desc = "Work first, play later"
            }
        }
        # Pacing modifiers
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = { geographical_region = world_middle_east }
                NOT = {
					exists = global_var:tournament_world_middle_east_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_middle_east_motivation"
            }
        }
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = {
					geographical_region = world_europe
					NOT = { geographical_region = world_europe_west }
				}
                NOT = {
					exists = global_var:tournament_world_europe_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_europe_motivation"
            }
        }
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = { geographical_region = world_europe_west }
                NOT = {
					exists = global_var:tournament_world_europe_west_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_europe_west_motivation"
            }
        }
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = { geographical_region = world_steppe }
                NOT = {
					exists = global_var:tournament_world_steppe_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_steppe_motivation"
            }
        }
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = { geographical_region = world_africa }
                NOT = {
					exists = global_var:tournament_world_africa_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_africa_motivation"
            }
        }
        if = {
            limit = {
                exists = capital_county
				capital_county.title_province = { geographical_region = world_india }
                NOT = {
					exists = global_var:tournament_world_india_motivation
				}
            }
            add = {
            	value = 100
            	desc = "tournament_world_india_motivation"
            }
        }
        # Negative Pacing modifiers
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = { geographical_region = world_middle_east }
				exists = global_var:tournament_world_middle_east_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_middle_east_demotivation"
            }
        }
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = {
					geographical_region = world_europe
					NOT = { geographical_region = world_europe_west }
				}
                exists = global_var:tournament_world_europe_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_europe_demotivation"
            }
        }
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = { geographical_region = world_europe_west }
                exists = global_var:tournament_world_europe_west_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_europe_west_demotivation"
            }
        }
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = { geographical_region = world_steppe }
                exists = global_var:tournament_world_steppe_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_steppe_demotivation"
            }
        }
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = { geographical_region = world_africa }
                exists = global_var:tournament_world_africa_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_africa_demotivation"
            }
        }
        if = {
            limit = {
				years_from_game_start > 100
                exists = capital_county
				capital_county.title_province = { geographical_region = world_india }
                exists = global_var:tournament_world_india_motivation
            }
            add = {
            	value = -45
            	desc = "tournament_world_india_demotivation"
            }
        }
        # Plague
		if = {
        	limit = {
        	    any_held_title = {
					tier = tier_county
        	        any_county_province = {
        	            any_province_epidemic = {
        	                count >= 1
        	                outbreak_intensity < apocalyptic
        	            }
        	        }
        	    }
        	    would_follow_social_distancing_value >= -1
        	}
        	add = {
        	    value = -60
        	    desc = "Plague in Domain"
        	}
    	}
    	else_if = {
    	    limit = {
    	        any_realm_county = {
    	            any_county_province = {
    	                any_province_epidemic = {
    	                    count >= 1
    	                    outbreak_intensity < apocalyptic
    	                }
    	            }
    	        }
    	        would_follow_social_distancing_value >= -1
    	    }
    	    add = {
    	        value = -20
    	        desc = "Plague in Top Realm"
    	    }
    	}
    	if = {
    	    limit = {
    	        any_held_title = {
					tier = tier_county
    	            any_county_province = {
    	                any_province_epidemic = {
    	                    count >= 1
    	                    outbreak_intensity = apocalyptic
    	                }
    	            }
    	        }
    	    }
    	    add = {
    	        value = -200
    	        desc = "Apocalyptic Plague in Domain"
    	    }
    	}
    	else_if = {
    	    limit = {
    	        any_held_title = {
					tier = tier_county
    	            any_county_province = {
    	                any_province_epidemic = {
    	                    count >= 1
    	                    outbreak_intensity = apocalyptic
    	                }
    	            }
    	        }
    	    }
    	    add = {
    	        value = -40
    	        desc = "Apocalyptic Plague in Top Realm"
    	    }
    	}
    	#we don't want the Pope to host tournaments
    	if = {
    		limit = {
    			government_has_flag = government_is_theocracy
    			has_title = title:k_papal_state
    		}
    		add = {
    			value = -1000
    			desc = "Is Pope"
    		}
    	}
		if = {
			limit = {
				is_ai = yes
				has_character_flag = tournament_ai_override
			}
			add = 10000
		}
	}

	ai_check_interval = 60

	ai_will_select_province = {
		# Base
		add = {
			value = 5
			desc = "base"
		}
		# Buildings
		if = {
			limit = { has_building_or_higher = jousting_lists_01 }
			add = {
				value = 1
				desc = activity_tournament_province_jousting_lists_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = war_camps_01 }
			add = {
				value = 1
				desc = activity_tournament_province_war_camps_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = military_camps_01 }
			add = {
				value = 1
				desc = activity_tournament_province_military_camps_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = archery_ranges_01 }
			add = {
				value = 1
				desc = activity_tournament_province_archery_ranges_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = blacksmiths_01 }
			add = {
				value = 1
				desc = activity_tournament_province_blacksmiths_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = royal_armory_01 }
			add = {
				value = 1
				desc = activity_tournament_province_royal_armory_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = hall_of_heroes_01 }
			add = {
				value = 1
				desc = activity_tournament_province_hall_of_heroes_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = barracks_01 }
			add = {
				value = 1
				desc = activity_tournament_province_barracks_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = regimental_grounds_01 }
			add = {
				value = 1
				desc = activity_tournament_province_regimental_grounds_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = smiths_01 }
			add = {
				value = 1
				desc = activity_tournament_province_smiths_01_desc
			}
		}
		if = {
			limit = { has_building_or_higher = stables_01 }
			add = {
				value = 1
				desc = activity_tournament_province_stables_01_desc
			}
		}
		if = {
			limit = {
				OR = {
					has_building_or_higher = generic_university
					has_building_or_higher = al_azhar_university
					has_building_or_higher = sankore_university
					has_building_or_higher = siena_university
					has_building_or_higher = nalanda_university
					has_building_or_higher = al_qarawiyyin_university_01
					has_building_or_higher = somapura_university_01
					has_building_or_higher = cluny_abbey_01
					has_building_or_higher = ashikaga_gakko_university		# Shogunate
					has_building_or_higher = kanesawa_bunko_library			# Shogunate
				}
			}
			add = {
				value = 1
				desc = activity_tournament_province_generic_university_desc
			}
		}
	}

	###################
	# PARAMETERS
	###################

	activity_group_type = grand

	is_single_location = yes

	wait_time_before_start = {
		# Fixed duration, because it is an open activity
		# Note: this determines other event firing times, please edit below value directly
		months = tournament_wait_time_before_start_months
	}
	max_guest_arrival_delay_time = {
		months = 1
	}

	cooldown = {
		years = 10
	}

	province_filter = domain

	is_location_valid = {
		has_holding = yes
	}

	province_description = {
		desc = activity_tournament_province_desc
		# +++ / always a good thing
		triggered_desc = {
			trigger = { 
				county.development_level >= great_development_level 
			}
			desc = activity_tournament_province_desc_development_great
		}
		# ++ / double plus good
		triggered_desc = {
			trigger = { 
				county.development_level >= good_development_level 
				NOT = {
					county.development_level >= great_development_level
				}
			}
			desc = activity_tournament_province_desc_development_good
		}
		# + / plus good
		triggered_desc = {
			trigger = { 
				county.development_level >= medium_development_level 
				NOT = {
					county.development_level >= good_development_level
				}
			}
			desc = activity_tournament_province_desc_development_ok
		}
		triggered_desc = {
			trigger = { has_building_or_higher = jousting_lists_01 }
			desc = activity_tournament_province_jousting_lists_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = blacksmiths_01 }
			desc = activity_tournament_province_blacksmiths_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = archery_ranges_01 }
			desc = activity_tournament_province_archery_ranges_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = royal_armory_01 }
			desc = activity_tournament_province_royal_armory_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = regimental_grounds_01 }
			desc = activity_tournament_province_regimental_grounds_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = stables_01 }
			desc = activity_tournament_province_stables_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = military_camps_01 }
			desc = activity_tournament_province_military_camps_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = barracks_01 }
			desc = activity_tournament_province_barracks_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = smiths_01 }
			desc = activity_tournament_province_smiths_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = war_camps_01 }
			desc = activity_tournament_province_war_camps_01_desc
		}
		triggered_desc = {
			trigger = { has_building_or_higher = hall_of_heroes_01 }
			desc = activity_tournament_province_hall_of_heroes_01_desc
		}
		triggered_desc = {
			trigger = {
				OR = {
					has_building_or_higher = generic_university
					has_building_or_higher = al_azhar_university
					has_building_or_higher = sankore_university
					has_building_or_higher = siena_university
					has_building_or_higher = nalanda_university
					has_building_or_higher = al_qarawiyyin_university_01
					has_building_or_higher = somapura_university_01
					has_building_or_higher = cluny_abbey_01
					has_building_or_higher = ashikaga_gakko_university		# Shogunate
					has_building_or_higher = kanesawa_bunko_library			# Shogunate
				}
			}
			desc = activity_tournament_province_generic_university_desc
		}
		triggered_desc = {
			trigger = {
				OR = {
					terrain = plains
					terrain = oasis
					terrain = steppe
				}
			}
			desc = activity_tournament_province_desc_terrain_good
		}
		# - / minus bad
		triggered_desc = {
			trigger = { county.development_level <= 5 }
			desc = activity_tournament_province_desc_development_bad
		}
		triggered_desc = {
			trigger = {
				OR = {
					terrain = desert
					terrain = forest
					terrain = taiga
					terrain = jungle
					terrain = hills
				}
			}
			desc = activity_tournament_province_desc_terrain_bad
		}
		# -- / double minus bad
		triggered_desc = {
			trigger = {
				OR = {
					terrain = mountains
					terrain = desert_mountains
				}
			}
			desc = activity_tournament_province_desc_terrain_verybad
		}
		# --- / triple minus bad
		triggered_desc = {
			trigger = {
				OR = {
					terrain = floodplains
					terrain = wetlands
				}
			}
			desc = activity_tournament_province_desc_terrain_terrible
		}
	}

	max_province_icons = 5

	province_score = {
		add = {
			value = county.development_level
			divide = 2
		}
		if = {
			limit = {
				has_building_or_higher = hall_of_heroes_01
			}
			add = 30
		}
		else_if = {
			limit = {
				OR = {
					has_building_or_higher = jousting_lists_01
					has_building_or_higher = blacksmiths_01
					has_building_or_higher = archery_ranges_01
					has_building_or_higher = royal_armory_01
				}
			}
			add = 20
		}
		if = {
			limit = {
				OR = {
					has_building_or_higher = barracks_01
					has_building_or_higher = smiths_01
					has_building_or_higher = regimental_grounds_01
					has_building_or_higher = stables_01
					has_building_or_higher = military_camps_01
					has_building_or_higher = war_camps_01
				}
			}
			add = 10
		}
		if = {
			limit = {
				OR = {
					terrain = plains
					terrain = oasis
					terrain = steppe
				}
			}
			add = 5
		}
		else_if = {
			limit = {
				OR = {
					terrain = desert
					terrain = forest
					terrain = taiga
					terrain = jungle
					terrain = hills
				}
			}
			add = -5
		}
		else_if = {
			limit = {
				OR = {
					terrain = mountains
					terrain = desert_mountains
				}
			}
			add = -15
		}
		else_if = {
			limit = {
				OR = {
					terrain = floodplains
					terrain = wetlands
				}
			}
			add = -30
		}
		if = {
			limit = {
				county.development_level <= 5
			}
			add = -5
		}
		else_if = {
			limit = {
				county.development_level >= medium_development_level
				NOT = {
					county.development_level >= good_development_level
				}
			}
			add = 10
		}
		else_if = {
			limit = {
				county.development_level >= good_development_level 
				NOT = {
					county.development_level >= great_development_level
				}
			}
			add = 20
		}
		else_if = {
			limit = {
				county.development_level >= great_development_level 
			}
			add = 30
		}
	}

	cost = {
		gold = {
			add = {
				add = {
					value = tournament_base_cost
					desc = tournament_base_cost
				}
				add = {
					value = tournament_base_cost
					multiply = activity_cost_scale_by_era
					subtract = tournament_base_cost
					desc = activity_cost_scale_by_era_desc
				}
				if = {
					limit = { scope:province ?= { has_building_or_higher = hall_of_heroes_01 } }
					add = {
						value = 0
						add = {
							value = tournament_base_cost
							add = {
								value = tournament_base_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_base_cost
							}
						}
						multiply = tournament_activity_cost_max_discount_value
						desc = tournament_activity_cost_max_discount_desc
					}
				}
				if = {
					limit = { scope:province ?= { has_building_or_higher = war_camps_01 } } 
					add = {
						value = 0
						add = {
							value = tournament_base_cost
							add = {
								value = tournament_base_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_base_cost
							}
						}
						multiply = tournament_activity_cost_discount_value
						desc = tournament_activity_cost_discount_war_camps_desc
					}
				}
				if = {
					limit = { scope:province ?= { has_building_or_higher = barracks_01 } }
					add = {
						value = 0
						add = {
							value = tournament_base_cost
							add = {
								value = tournament_base_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_base_cost
							}
						}
						multiply = tournament_activity_cost_discount_value
						desc = tournament_activity_cost_discount_barracks_desc
					}
				}
				# Some people get one free.
				if = {
					limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
					multiply = 0
				}
				min = 0
				if = {
					limit = {
						is_ai = yes
						has_character_flag = tournament_ai_override
					}
					multiply = 0
				}
			}
		}
	}

	ui_predicted_cost = {
		# All costs are balanced on County/Early Era starting point
		gold = {
			# Base Cost
			add = {
				value = tournament_base_cost
				desc = tournament_base_cost
			}
			# Scale by Era
			multiply = {
				value = activity_cost_scale_by_era
				desc = activity_cost_scale_by_era_desc
			}

			# OPTIONS
			add = {
				value = { # Calculate the average of the three option costs
					value = 0 
					add = tournament_option_bad_cost
					add = tournament_option_normal_cost
					add = tournament_option_good_cost
					add = tournament_option_very_good_cost
					#add = tournament_option_splendid_cost # Prediction becomes very costly with this
					# Scale by era 
					multiply = activity_cost_scale_by_era
					# Divide by total number of option levels to get an average option cost
					divide = 4
				}
				# Mult 2 because there are two option categories with the same costs 
				multiply = 2
			}

			# CONTESTS 
			add = {
				value = 113 # Average contest cost in the Early Era
				# Scale average cost by era
				multiply = activity_cost_scale_by_era
				# Try to account for the average number of planned contest based on era
				if = {
					limit = {
						root.culture = { has_cultural_era_or_later = culture_era_late_medieval }
					}
					multiply = 3 # average number to be hosted in the late era
				}
				else_if = {
					limit = {
						root.culture = { has_cultural_era_or_later = culture_era_high_medieval }
					}
					multiply = 2 # average number to be hosted in the high era
				}
				else = {
					multiply = 1 # otherwise, we assume 1
				}
			}

			# Some people get a freebie
			if = {
				limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
				multiply = 0
			}
			
			# Make it a multiple of 5 (rounded up)
			divide = 5
			ceiling = yes 
			multiply = 5
		}
	}

	# Guests bring knights & close family
	travel_entourage_selection = {
		weight = {
			value = standard_travel_entourage_additions
			if = {
				limit = {
					bannable_serving_diarch_trigger = no
					OR = {
						is_knight_of = scope:owner
						scope:owner.court_position:champion_court_position ?= this
					}
					trigger_if = {
						limit = { is_landed = yes }
						highest_held_title_tier = tier_barony
					}
					is_available = yes
				}
				add = 100
			}
			if = {
				limit = {
					bannable_serving_diarch_trigger = no
					is_landed = no
					OR = {
						is_spouse_of = scope:owner
						is_close_family_of = scope:owner
					}
					is_available = yes
				}
				add = 50
			}
		}

		max = 15
		ai_max = 3
		invite_rule_order = 1
   	}

	conclusion_description = {
		desc = tournament_conclusion_general
		desc = {
			triggered_desc = {
				trigger = {
					exists = scope:activity.var:melee_winner
				}
				desc = tournament_conclusion_winner_melee
			}
			triggered_desc = {
				trigger = {
					exists = scope:activity.var:archery_winner
				}
				desc = tournament_conclusion_winner_archery
			}
			triggered_desc = {
				trigger = {
					exists = scope:activity.var:horse_race_winner
				}
				desc = tournament_conclusion_winner_horse_race
			}
			triggered_desc = {
				trigger = { exists = var:recital_winner }
				desc = tournament_conclusion_winner_recital
			}
			triggered_desc = {
				trigger = { exists = var:joust_winner }
				desc = tournament_conclusion_winner_joust
			}
			triggered_desc = {
				trigger = { exists = var:wrestling_winner }
				desc = tournament_conclusion_winner_wrestling
			}
			triggered_desc = {
				trigger = { exists = var:duel_winner }
				desc = tournament_conclusion_winner_duel
			}
			triggered_desc = {
				trigger = { exists = var:board_game_winner }
				desc = tournament_conclusion_winner_board_game
			}
		}
	}

   	window_characters = {
		player = {
			camera = camera_body
			
			effect = {
				scope:player ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = idle
			}
		}
		host = {
			camera = camera_body
			
			effect = {
				scope:host ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = happiness
			}
		}
		melee_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:melee_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = sword_coup_degrace
			}
		}
		archery_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:archery_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = hunting_shortbow_rest_arrow_default
			}
		}
		horse_race_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:horse_race_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = throne_room_cheer_1
			}
		}
		recital_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:recital_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = ecstasy
			}
		}
		joust_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:joust_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = jockey_victory
			}
		}
		duel_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:duel_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = marshal
			}
		}
		board_game_winner = {
			camera = camera_body
			
			effect = {
				scope:activity.var:board_game_winner ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = chess_certain_win
			}
		}
	}

	###################
	# GRAPHICS
	###################

	map_entity = { #MENA entity
		trigger = {
			activity_location.culture = {
				OR = {
					has_graphical_iranian_culture_group_trigger = yes
					has_graphical_mena_culture_group_trigger = yes
					has_graphical_steppe_culture_group_trigger = yes
					has_graphical_african_culture_group_trigger = yes
				}
			}
		}
		reference = "building_mena_tournament_01_entity"
	}

	map_entity = {
		trigger = {
			activity_location.culture = {
				has_graphical_india_culture_group_trigger = yes
			}
		}
		reference = "building_indian_tournament_01_entity"
	}

	map_entity = "building_western_tournament_01_entity"

	background = {
		texture = "gfx/interface/illustrations/activity_header_backgrounds/activity_tournament.dds"
		environment = "environment_jerusalem"
		ambience = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_tournament_locale"
	}

	locale_background = { #MENA Background
		trigger = {
			activity_location = {
				OR = {
					terrain = desert
					terrain = desert_mountains
					terrain = drylands
					terrain = oasis
					terrain = floodplains
					AND = {
						OR = {
							terrain = mountains
							terrain = hills
							terrain = wetlands
						}
						OR = {
							geographical_region = world_middle_east
							geographical_region = world_africa_east
						}
					}
				}
			}
		}
		texture = "gfx/interface/illustrations/activity_backgrounds/tournament_terrain_mena.dds"
		environment = "environment_jerusalem"
		ambience = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_tournament_locale"
		music = "tournamentmena_cue"
	}

	locale_background = { #India Background
		trigger = {
			activity_location.culture = {
				has_graphical_india_culture_group_trigger = yes
			}
			activity_location = {
				OR = {
					terrain = jungle
					AND = {
						OR = {
							terrain = mountains
							terrain = forest
							terrain = hills
							terrain = plains
							terrain = wetlands
							terrain = farmlands
						}
						OR = {
							geographical_region = world_india
							geographical_region = world_burma
						}
					}
					AND = {
						terrain = forest
						OR = {
							geographical_region = world_africa_west
						}
					}
				}
			}
		}
		texture = "gfx/interface/illustrations/activity_backgrounds/tournament_terrain_jungle.dds"
		environment = "environment_jerusalem"
		ambience = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_tournament_locale"
		music = "tournamentindia_cue"
	}

	locale_background = { #Western Background
		trigger = {
		}
		texture = "gfx/interface/illustrations/activity_backgrounds/tournament_terrain_europe_farmland.dds"
		environment = "environment_jerusalem"
		ambience = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_tournament_locale"
		music = "tournamentwest_cue"
	}

	###################
	# OPTIONS
	###################

	options = {
		tournament_option_accommodations = {
			tournament_accommodations_bad = {

				cost = {
					gold = {
						add = {
							desc = tournament_accommodations_bad
							add = {
								value = tournament_option_bad_cost
								desc = tournament_accommodations_bad
							}
							add = {
								value = tournament_option_bad_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_bad_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 30
					}
					if = {
						limit = {
							has_trait = greedy
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = lazy
								has_trait = temperate
								has_trait = content
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = humble
						}
						add = 10
					}
				}
			}
			tournament_accommodations_normal = {
				default = yes

				cost = {
					gold = {
						add = {
							desc = tournament_accommodations_normal
							add = {
								value = tournament_option_normal_cost
								desc = tournament_accommodations_normal
							}
							add = {
								value = tournament_option_normal_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_normal_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 50
					}
				}
			}
			tournament_accommodations_good = {

				on_start = {
					scope:host = {
						add_prestige = medium_prestige_value
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 30
					}
					if = {
						limit = {
							has_trait = generous
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = diligent
								has_trait = gluttonous
								has_trait = ambitious
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = arrogant
						}
						add = 10
					}
				}

				cost = {
					gold = {	
						add = {
							desc = tournament_accommodations_good
							add = {
								value = tournament_option_good_cost
								desc = tournament_accommodations_good
							}
							add = {
								value = tournament_option_good_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_good_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
			}
		}

		tournament_option_prizes = {
			tournament_prizes_bad = {
				cost = {
					gold = {
						add = {
							desc = tournament_prizes_bad
							add = {
								value = tournament_option_bad_cost
								desc = tournament_prizes_bad
							}
							add = {
								value = tournament_option_bad_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_bad_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 30
					}
					if = {
						limit = {
							has_trait = greedy
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = ambitious
								has_trait = content
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = humble
						}
						add = 10
					}
				}
			}
			tournament_prizes_normal = {

				cost = {
					gold = {
						add = {
							desc = tournament_prizes_normal
							add = {
								value = tournament_option_normal_cost
								desc = tournament_prizes_normal
							}
							add = {
								value = tournament_option_normal_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_normal_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = {
						value = 40
						desc = "Base test value"
					}
				}
			}
			tournament_prizes_good = {
				default = yes
				
				cost = {
					gold = {
						add = {
							desc = tournament_prizes_good
							add = {
								value = tournament_option_good_cost
								desc = tournament_prizes_good
							}
							add = {
								value = tournament_option_good_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_good_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 40
					}
					if = {
						limit = {
							has_trait = generous
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = gluttonous
								has_trait = ambitious
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = arrogant
						}
						add = 10
					}
					if = {
						limit = {
							ai_greed <= 0
							gold >= major_gold_value
						}
						add = 30
					}
				}
			}
			tournament_prizes_very_good = {
				
				cost = {
					gold = {
						add = {
							desc = tournament_prizes_very_good
							add = {
								value = tournament_option_very_good_cost
								desc = tournament_prizes_very_good
							}
							add = {
								value = tournament_option_very_good_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_very_good_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 40
					}
					if = {
						limit = {
							has_trait = generous
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = gluttonous
								has_trait = ambitious
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = arrogant
						}
						add = 10
					}
					if = {
						limit = {
							ai_greed <= 0
							gold >= major_gold_value
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								gold <= major_gold_value
								gold <= tournament_option_very_good_era_scaled_cost
							}
						}
						multiply = 0
					}
				}
			}
			tournament_prizes_splendid = {
				
				cost = {
					gold = {
						add = {
							desc = tournament_prizes_splendid
							add = {
								value = tournament_option_splendid_cost
								desc = tournament_prizes_splendid
							}
							add = {
								value = tournament_option_splendid_cost
								multiply = activity_cost_scale_by_era
								subtract = tournament_option_splendid_cost
								desc = activity_cost_scale_by_era_desc
							}
							if = {
								limit = {
									has_perk = journey_planner_perk
								}
								multiply = {
									value = voyager_journey_planner_value
									desc = journey_planner_perk_name
								}
							}
							# Some people get one free.
							if = {
								limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
								multiply = 0
							}
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}

				ai_will_do = {
					value = 0
					add = { #Base amount
						value = 50
					}
					if = {
						limit = {
							has_trait = generous
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								has_trait = gluttonous
								has_trait = ambitious
							}
						}
						add = 15
					}
					if = {
						limit = {
							has_trait = arrogant
						}
						add = 10
					}
					if = {
						limit = {
							ai_greed <= 0
							gold >= massive_gold_value
						}
						add = 30
					}
					if = {
						limit = {
							OR = {
								gold <= massive_gold_value
								gold <= tournament_option_splendid_era_scaled_cost
							}
						}
						multiply = 0
					}
				}
			}
		}
	}

	###################
	# PHASES
	###################

	num_pickable_phases = tournament_active_phase_count_value

	phases = {
		############
		### TEAM ###
		############

		# Always 20 qualified
		# Winning team based on averaged PtV

		### GUEST SUBSETS
		# winner (10)
		# second_place (10)
		# team_alpha (10)
		# team_beta (10)
		# qualified (20)
		# competed
		# spectator

		# MELEE
		# contest_foot_aptitude_value
		tournament_phase_melee = {
			can_pick = { #Should be able to host this basically anywhere in Europe and certain areas outside too
				NOT = { government_has_flag = government_is_tribal }
				trigger_if = {
					limit = {
						capital_province = { geographical_region = world_europe }
					}
				}
				trigger_else = {
					culture = { has_cultural_parameter = can_host_joust_contests }
				}
			}

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_standard_duration_months } }
					tournament_contest_team_composition_effect = { CONTEST = melee SKILL = melee } # Create two teams
					tournament_team_pulse_effect = { CONTEST = melee }
				}
				tournament_add_as_spectator_effect = yes
			}

			on_end = {
				if = {
					limit = { this = scope:activity.activity_host }
					tournament_contest_team_winner_effect = { CONTEST = melee }
				}
			}

			ai_will_do = {
				add = {
					value = 100
					desc = contest_ai_base
				}
				add = {
					value = contest_foot_aptitude_value
					desc = contest_foot_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = foot
							value >= 25
						}
					}
					add = {
						value = 25
						desc = tourney_participant_foot
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_warrior_culture }
					}
					add = {
						value = 25
						desc = tradition_warrior_culture_name
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_martial_admiration }
					}
					add = {
						value = 25
						desc = tradition_martial_admiration_name
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_chanson_de_geste }
					}
					add = {
						value = 25
						desc = tradition_chanson_de_geste_name
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_central_germanic
								has_cultural_pillar = heritage_frankish
								has_cultural_pillar = heritage_iberian
								has_cultural_pillar = heritage_west_germanic
								has_cultural_pillar = heritage_west_slavic
							}
						}
					}
					add = {
						value = 15
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_base_tribal_cost
							desc = tournament_melee_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_base_cost #Base level for Contest
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_melee_cost
						}
						add = {
							value = tournament_contest_base_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_base_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = royal_armory_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_melee_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = stables_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_low_discount_value
								desc = tournament_melee_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		############
		### TURN ###
		############

		# Infinite qualified
		# Winner and podium placements weighted by PtV

		### GUEST SUBSETS
		# winner
		# second_place
		# third_place
		# qualified
		# competed
		# spectator

		# ARCHERY
		# contest_archery_aptitude_value
		tournament_phase_archery = {			
			is_shown = {}

			can_pick = {} #Deliberately empty; should be able to host this anywhere

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_standard_duration_months } }
					tournament_contest_turn_composition_effect = { CONTEST = archery SKILL = archery } # Create competitor list and log entry
					tournament_turn_pulse_effect = { CONTEST = archery }
				}
				tournament_add_as_spectator_effect = yes
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_turn_winner_effect = { CONTEST = archery }
				}
			}

			ai_will_do = {
				add = {
					value = 75
					desc = contest_ai_base
				}
				add = {
					value = contest_archery_aptitude_value
					desc = contest_archery_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = bow
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_bow
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_land_of_the_bow }
					}
					add = {
						value = 50
						desc = tradition_land_of_the_bow_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_longbow_competitions }
					}
					add = {
						value = 50
						desc = tradition_longbow_competitions_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_hunters }
					}
					add = {
						value = 50
						desc = tradition_hunters_name
					}
				}
				if = {
					limit = { religion = religion:islam_religion }
					add = {
						value = 50
						desc = contest_ai_maysir
					}
				}
				if = {
					limit = { government_has_flag = government_is_tribal }
					add = {
						value = 50
						desc = tribal_government
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_akan
								has_cultural_pillar = heritage_brythonic
								has_cultural_pillar = heritage_central_african
								has_cultural_pillar = heritage_east_african
								has_cultural_pillar = heritage_goidelic
								has_cultural_pillar = heritage_west_african
								has_cultural_pillar = heritage_somalian
								has_cultural_pillar = heritage_indo_aryan
								has_cultural_pillar = heritage_magyar
								has_cultural_pillar = heritage_mongolic
								has_cultural_pillar = heritage_yoruba
								has_cultural_pillar = heritage_sahelian
							}
						}
					}
					add = {
						value = 15
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_base_tribal_cost
							desc = tournament_archery_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_base_cost #Base level for Contest
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_archery_cost
						}
						add = {
							value = tournament_contest_base_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_base_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = archery_ranges_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_archery_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = military_camps_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_archery_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		# RECITAL
		# contest_recital_aptitude_value
		tournament_phase_recital = {
			can_pick = {} #Deliberately empty; recitals and speaking contests were widely seen across the world

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_standard_duration_months } }
					tournament_contest_turn_composition_effect = { CONTEST = recital SKILL = recital } # Create competitor list and log entry
					tournament_turn_pulse_effect = { CONTEST = recital }
				}
				tournament_add_as_spectator_effect = yes
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_turn_winner_effect = { CONTEST = recital }
				}
			}

			ai_will_do = {
				add = {
					value = 50
					desc = contest_ai_base
				}
				add = {
					value = contest_recital_aptitude_value
					desc = contest_recital_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = wit
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_wit
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_fp1_northern_stories }
					}
					add = {
						value = 25
						desc = tradition_fp1_northern_stories_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_poetry }
					}
					add = {
						value = 50
						desc = tradition_poetry_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_storytellers }
					}
					add = {
						value = 50
						desc = tradition_storytellers_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_pillar = heritage_brythonic }
					}
					add = {
						value = 25
						desc = heritage_brythonic_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_pillar = heritage_goidelic }
					}
					add = {
						value = 25
						desc = heritage_goidelic_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_pillar = heritage_north_germanic }
					}
					add = {
						value = 25
						desc = heritage_north_germanic_name
					}
				}
				if = {
					limit = { government_has_flag = government_is_tribal }
					add = {
						value = 50
						desc = tribal_government
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_brythonic
								has_cultural_pillar = heritage_goidelic
								has_cultural_pillar = heritage_latin
							}
						}
					}
					add = {
						value = 25
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_cheap_tribal_cost
							desc = tournament_recital_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_cheap_cost #Very cheap, requires basically nothing
							desc = tournament_recital_cost
						}
						add = {
							value = tournament_contest_cheap_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_cheap_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									OR = {
										has_building_or_higher = generic_university
										has_building_or_higher = al_azhar_university
										has_building_or_higher = sankore_university
										has_building_or_higher = siena_university
										has_building_or_higher = nalanda_university
										has_building_or_higher = ashikaga_gakko_university		# Shogunate
										has_building_or_higher = kanesawa_bunko_library			# Shogunate
									}
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_cheap_cost
									add = {
										value = tournament_contest_cheap_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_cheap_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_recital_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = warrior_lodges_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_cheap_cost
									add = {
										value = tournament_contest_cheap_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_cheap_cost
									}
								}
								multiply = tournament_contest_cost_low_discount_value
								desc = tournament_recital_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		##############
		### VERSUS ###
		##############

		# Always 8 qualified
		# Single winner by knock out 'tournament'

		### GUEST SUBSETS
		# winner
		# second_place
		# finalist (2)
		# semi_finalist (4)
		# qualified (8)
		# contestant
		# spectator

		# JOUST
		# contest_horse_aptitude_value
		tournament_phase_joust = {			
			is_shown = {}

			can_pick = {
				NOT = { government_has_flag = government_is_tribal }
				trigger_if = {
					limit = {
						capital_province = { geographical_region = world_europe }
					}
				}
				trigger_else = {
					culture = { has_cultural_parameter = can_host_joust_contests }
				}
				scope:province = { #Horses do not like big hills or marshes
					NOR = {
						terrain = mountains
						terrain = desert_mountains
						terrain = wetlands
					} 
				}
				scope:province.culture = { #Ensure the honses are properly shod and such
					AND = {
						has_innovation = innovation_horseshoes
						has_innovation = innovation_arched_saddle
					}
				}
			}

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_versus_duration_months } }
					tournament_contest_versus_composition_effect = { CONTEST = joust SKILL = horse } # Create competitor list and log entry
				}
				tournament_add_as_spectator_effect = yes
				# Achievements.
				if = {
					limit = {
						exists = global_var:started_sir_lance_a_lot_achievement
						is_ai = no
						has_trait_xp = {
							trait = tourney_participant
							track = horse
							value >= 100
						}
					}
					set_global_variable = {
						name = achieved_sir_lance_a_lot_achievement
						value = yes
					}
				}
			}

			on_weekly_pulse = {
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_versus_winner_effect = { CONTEST = joust }
				}
				# Achievements.
				if = {
					limit = {
						exists = global_var:started_a_knights_tale_achievement
						is_ai = no
						scope:activity = {
							any_guest_subset = {
								name = winner
								phase = tournament_phase_joust
								is_knight_of = root
								NOT = { exists = dynasty }
							}
						}
					}
					scope:activity = {
						random_guest_subset = {
							name = winner
							phase = tournament_phase_joust
							limit = {
								is_knight_of = root
								NOT = { exists = dynasty }
							}
							save_scope_as = cheevo_knight
						}
					}
					add_to_variable_list = {
						name = candidate_a_knights_tale_achievement
						target = scope:cheevo_knight
					}
				}
			}

			ai_will_do = {
				add = {
					value = 100 # Popular contest
					desc = contest_ai_base
				}
				add = {
					value = contest_horse_aptitude_value
					desc = contest_horse_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = horse
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_horse
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_chivalry }
					}
					add = {
						value = 25
						desc = tradition_chivalry_name
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_martial_admiration }
					}
					add = {
						value = 25
						desc = tradition_martial_admiration_name
					}
				}
				if = {
					limit = { 
						culture = { has_cultural_tradition = tradition_chanson_de_geste }
					}
					add = {
						value = 25
						desc = tradition_chanson_de_geste_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_era_or_later = culture_era_high_medieval }
					}
					add = {
						value = 50
						desc = culture_era_high_medieval
					}
				}
				if = {
					limit = {
						culture = { has_cultural_era_or_later = culture_era_late_medieval }
					}
					add = {
						value = 50
						desc = culture_era_late_medieval
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_frankish
								has_cultural_pillar = heritage_west_germanic
								has_cultural_pillar = heritage_central_germanic
							}
						}
					}
					add = {
						value = 15
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_expensive_tribal_cost
							desc = tournament_joust_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_expensive_cost #Very cheap, requires basically nothing
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_joust_cost
						}
						add = {
							value = tournament_contest_expensive_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_expensive_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = jousting_lists_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_expensive_cost
									add = {
										value = tournament_contest_expensive_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_expensive_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_joust_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = stables_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_expensive_cost
									add = {
										value = tournament_contest_expensive_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_expensive_cost
									}
								}
								multiply = tournament_contest_cost_low_discount_value
								desc = tournament_joust_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		# WRESTLING
		# contest_pugilism_aptitude_value
		tournament_phase_wrestling = {			
			is_shown = {}

			can_pick = { #Available to all, given how prolific it is.
			}

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_versus_duration_months } }
					tournament_contest_versus_composition_effect = { CONTEST = wrestling SKILL = pugilism } # Create competitor list and log entry
				}
				tournament_add_as_spectator_effect = yes
			}

			on_weekly_pulse = {
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_versus_winner_effect = { CONTEST = wrestling }
				}
			}

			ai_will_do = {
				add = {
					value = 75
					desc = contest_ai_base
				}
				add = {
					value = contest_pugilism_aptitude_value
					desc = contest_pugilism_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = foot
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_foot
					}
				}
				if = {
					limit = { government_has_flag = government_is_tribal }
					add = {
						value = 50
						desc = tribal_government
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_akan
								has_cultural_pillar = heritage_arabic
								has_cultural_pillar = heritage_balto_finnic
								has_cultural_pillar = heritage_berber
								has_cultural_pillar = heritage_burman
								has_cultural_pillar = heritage_central_african
								has_cultural_pillar = heritage_dravidian
								has_cultural_pillar = heritage_east_african
								has_cultural_pillar = heritage_east_slavic
								has_cultural_pillar = heritage_goidelic
								has_cultural_pillar = heritage_west_african
								has_cultural_pillar = heritage_indo_aryan
								has_cultural_pillar = heritage_yoruba
								has_cultural_pillar = heritage_north_germanic
								has_cultural_pillar = heritage_sahelian
								has_cultural_pillar = heritage_senegambian
								has_cultural_pillar = heritage_ugro_permian
								has_cultural_pillar = heritage_west_slavic
							}
						}
					}
					add = {
						value = 15
						desc = game_rule_category_culture
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_burman
								has_cultural_pillar = heritage_iranian
								has_cultural_pillar = heritage_mongolic
							}
						}
					}
					add = {
						value = 30
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_base_tribal_cost
							desc = tournament_wrestling_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_base_cost #Very cheap, requires basically nothing
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_wrestling_cost
						}
						add = {
							value = tournament_contest_base_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_base_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = regimental_grounds_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_wrestling_contest_cost_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		# DUEL
		# contest_foot_aptitude_value
		tournament_phase_duel = {			
			is_shown = {}

			can_pick = {} #Deliberate; one-on-one martial contests were pretty widespread

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_versus_duration_months } }
					tournament_contest_versus_composition_effect = { CONTEST = duel SKILL = foot } # Create competitor list and log entry
				}
				tournament_add_as_spectator_effect = yes
			}

			on_weekly_pulse = {
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_versus_winner_effect = { CONTEST = duel }
				}
			}

			ai_will_do = {
				add = {
					value = 50
					desc = contest_ai_base
				}
				add = {
					value = contest_foot_aptitude_value
					desc = tourney_participant_foot
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = foot
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_foot
					}
				}
				if = {
					limit = {
						culture = { has_cultural_era_or_later = culture_era_early_medieval }
					}
					add = {
						value = 25
						desc = culture_era_early_medieval
					}
				}
				if = {
					limit = {
						culture = { has_cultural_era_or_later = culture_era_high_medieval }
					}
					add = {
						value = 25
						desc = culture_era_high_medieval
					}
				}
				if = {
					limit = {
						culture = { has_cultural_era_or_later = culture_era_late_medieval }
					}
					add = {
						value = 25
						desc = culture_era_late_medieval
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_base_tribal_cost
							desc = tournament_duel_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_base_cost #Very cheap, requires basically nothing
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_duel_cost
						}
						add = {
							value = tournament_contest_base_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_base_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = blacksmiths_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_duel_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = smiths_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_duel_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		# BOARD GAME
		# contest_board_game_aptitude_value
		tournament_phase_board_game = {			
			is_shown = {}

			can_pick = {} #Board games in all different forms were seen all over the globe

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_versus_duration_months } }
					tournament_contest_versus_composition_effect = { CONTEST = board_game SKILL = board_game } # Create competitor list and log entry
				}
				tournament_add_as_spectator_effect = yes
			}

			on_weekly_pulse = {
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_versus_winner_effect = { CONTEST = board_game }
				}
			}

			ai_will_do = {
				add = {
					value = 50
					desc = contest_ai_base
				}
				add = {
					value = contest_board_game_aptitude_value
					desc = contest_board_game_aptitude_value
				}
				if = {
					limit = {
						has_trait = tourney_participant
						has_trait_xp = {
							trait = tourney_participant
							track = wit
							value >= 25
						}
					}
					add = {
						value = 50
						desc = tourney_participant_wit
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_fp2_strategy_gamers }
					}
					add = {
						value = 50
						desc = tradition_fp2_strategy_gamers_name
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_base_tribal_cost
							desc = tournament_board_games_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_base_cost #Very cheap, requires basically nothing
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_board_games_cost
						}
						add = {
							value = tournament_contest_base_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_base_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									OR = {
										has_building_or_higher = generic_university
										has_building_or_higher = al_azhar_university
										has_building_or_higher = sankore_university
										has_building_or_higher = siena_university
										has_building_or_higher = nalanda_university
										has_building_or_higher = ashikaga_gakko_university		# Shogunate
										has_building_or_higher = kanesawa_bunko_library			# Shogunate
									}
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_board_games_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = warrior_lodges_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_base_cost
									add = {
										value = tournament_contest_base_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_base_cost
									}
								}
								multiply = tournament_contest_cost_low_discount_value
								desc = tournament_board_games_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		#############
		### RACE  ###
		#############

		# Always 10 qualified
		# Winner and podium placements weighted by PtV

		### GUEST SUBSETS
		# winner
		# second_place
		# third_place
		# qualified (10)
		# contestant
		# spectator

		# HORSE RACE
		# contest_horse_race_aptitude_value
		tournament_phase_horse_race = {			
			is_shown = { # Appears to have not really been a thing in West Africa buuut we need a least a lil bit of honse there, so
				#NOT = {
				#	location = {
				#		geographical_region = world_west_africa
				#	}
				#}
			}

			can_pick = {}

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = tournament_qualification_duration_weeks } }
				}
			}

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { months = tournament_standard_duration_months } }
					tournament_contest_race_composition_effect = { CONTEST = horse_race SKILL = horse_race } # Create competitor list and log entry
					tournament_race_pulse_effect = { CONTEST = horse_race }
				}
				tournament_add_as_spectator_effect = yes
				if = {
					limit = {
						is_in_guest_subset = { name = qualified }
					}
					trigger_event = {
						id = ep2_tournament_events.4500
						days = 1
					}
				}
			}

			on_end = {
				if = {
					limit = { this = scope:host }
					tournament_contest_race_winner_effect = { CONTEST = horse_race }
				}
			}

			ai_will_do = {
				add = {
					value = 50
					desc = contest_ai_base
				}
				add = {
					value = contest_horse_race_aptitude_value
					desc = contest_horse_race_aptitude_value
				}
				if = {
					limit = { has_trait = profligate }
					add = {
						value = 25
						desc = profligate
					}
				}
				if = {
					limit = {
						culture = { has_cultural_pillar = heritage_arabic }
					}
					add = {
						value = 50
						desc = heritage_arabic_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_roman_legacy }
					}
					add = {
						value = 50
						desc = tradition_roman_legacy_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_ep3_imperial_tagmata }
					}
					add = {
						value = 50
						desc = tradition_ep3_imperial_tagmata_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_ep3_indomitable_azatani }
					}
					add = {
						value = 50
						desc = tradition_ep3_indomitable_azatani_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_horse_lords }
					}
					add = {
						value = 75
						desc = tradition_horse_lords_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_horse_breeder }
					}
					add = {
						value = 75
						desc = tradition_horse_breeder_name
					}
				}
				if = {
					limit = {
						culture = { has_cultural_tradition = tradition_pastoralists }
					}
					add = {
						value = 75
						desc = tradition_pastoralists_name
					}
				}
				if = {
					limit = { religion = religion:islam_religion }
					add = {
						value = 50
						desc = contest_ai_maysir
					}
				}
				if = {
					limit = { 
						culture = { 
							OR = {
								has_cultural_pillar = heritage_magyar
								has_cultural_pillar = heritage_mongolic
								has_cultural_pillar = heritage_turkic
							}
						}
					}
					add = {
						value = 15
						desc = game_rule_category_culture
					}
				}
			}

			cost = {
				prestige = {
					if = {
						limit = { government_has_flag = government_is_tribal }
						add = {
							value = tournament_contest_horse_tribal_cost
							desc = tournament_horse_racing_cost
							if = {
								limit = {
									is_ai = yes
									has_character_flag = tournament_ai_override
								}
								multiply = 0
							}
						}
					}
				}
				gold = {
					add = {
						add = {
							value = tournament_contest_horse_cost #Very cheap, requires basically nothing
							if = {
								limit = { government_has_flag = government_is_tribal }
								divide = 2
							}
							desc = tournament_horse_racing_cost
						}
						add = {
							value = tournament_contest_horse_cost
							multiply = activity_cost_scale_by_era
							subtract = tournament_contest_horse_cost
							desc = activity_cost_scale_by_era_desc
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = jousting_lists_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_horse_cost
									add = {
										value = tournament_contest_horse_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_horse_cost
									}
								}
								multiply = tournament_contest_cost_discount_value
								desc = tournament_horse_racing_contest_cost_discount_desc
							}
						}
						if = { #building reducing the cost
							limit = {
								scope:province ?= {
									has_building_or_higher = stables_01
								}
							}
							add = {
								value = 0
								add = {
									value = tournament_contest_horse_cost
									add = {
										value = tournament_contest_horse_cost
										multiply = activity_cost_scale_by_era
										subtract = tournament_contest_horse_cost
									}
								}
								multiply = tournament_activity_cost_discount_value
								desc = tournament_horse_racing_contest_cost_low_discount_desc
							}
						}
						# Some people get one free.
						if = {
							limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
							multiply = 0
						}
						min = 0
						if = {
							limit = {
								is_ai = yes
								has_character_flag = tournament_ai_override
							}
							multiply = 0
						}
					}
				}
			}
		}

		#################
		### CEREMONY  ###
		#################

		tournament_phase_ceremony = {
			is_predefined = yes

			order = 2

			location_source = first_picked_phase

			on_enter_phase = {
				if = {
					limit = { this = scope:host }
					# No passive state for the ceremony, just jump right into it
					scope:activity = { progress_activity_phase_after = { days = 1 } }
				}
			}

			on_phase_active = {
				# Save intent target for final summary event 
				if = {
					limit = { this = scope:host }
					scope:activity = { progress_activity_phase_after = { weeks = 2 } }
					# Trigger summary event
					trigger_event = {
						id = tournament_events.0151
						weeks = 1
					}
				}
			}
		}
	}
	
	###################
	# LOCALES
	###################

	locales = {
		tourney_grounds = {
			locales = { tournament_locale_tournament_grounds }
		}
		campsite = {
			locales = { tournament_locale_visitor_camp }
		}
		entertainment = {
			locales = { tournament_locale_tavern }
		}
		town = {
			locales = { tournament_locale_settlement }
		}
		religious = {
			locales = { tournament_locale_religious_building }
		}
		shop_2 = {
			locales = { tournament_locale_artisans }
		}
	}
	locale_cooldown = { days = 20 }
	auto_select_locale_cooldown = { days = 60 }
	early_locale_opening_duration = { days = 75 }

	###################
	# GUEST HANDLING
	###################

	open_invite = yes
	
	max_guests = 250
	
	can_be_activity_guest = {
		is_adult = yes
		in_diplomatic_range = scope:host
		is_pool_guest = no
	}

	host_intents = {
		intents = { reduce_stress_intent win_contest_intent recruit_knights_intent murder_attendee_intent woo_attendee_intent befriend_attendee_intent }
		default = reduce_stress_intent
		player_defaults = { win_contest_intent reduce_stress_intent }
	}

	guest_intents = {
		intents = { reduce_stress_intent win_contest_intent recruit_knights_intent murder_attendee_intent woo_attendee_intent befriend_attendee_intent }
		default = reduce_stress_intent
		player_defaults = { win_contest_intent reduce_stress_intent }
	}

	guest_join_chance = {
		base = 5
		base_activity_modifier = yes

		# Scripted Modifiers
		activity_guest_shared_ai_accept_modifier = yes
		activity_guest_health_ai_accept_modifier = yes
		activity_guest_tournament_ai_accept_modifier = yes
		# Blademaster Accolade increasing invite acceptance
		modifier = {
   		   scope:host = {
   		   		any_active_accolade = {
					has_accolade_parameter = acclaimed_knight_tournament_invite_acceptance_bonus
				}
   		   }
   		   add = accolade_tournament_invite_acceptance_value
   		}
		modifier = {
   		   government_has_flag = government_is_mercenary
   		   add = -5000
   		}
	}

	guest_subsets = {
		contestant # Potentially eligible competitor - healthy & martial sex (if relevant)
		spectator # Ineligible for contest - unhealthy/non-martial sex/excluded
		qualified # Actively qualified and competing - used as equivalent of quarter_finalist for VERSUS
		third_place # Won third place - only used in TURN/RACE
		second_place # Won second place - not used in TEAM
		winner # Won contest overall - contains all winners in TEAM
		semi_finalist # Reached contest semi-finals - ALWAYS 4
		finalist # Reached contest finals - ALWAYS 2
		team_alpha # MAX 10
		team_beta # MAX 10
		team_alpha_captain # 1
		team_beta_captain # 1
	}

	special_guests = {
		special_guest_champion = {
			is_required = no
			can_pick = {
				trigger_if = {
					limit = {
						scope:host = { employs_court_position = champion_court_position }
					}
					employer = scope:host
					has_court_position = champion_court_position
				}
				trigger_else_if = {
					limit = {
						scope:host = { can_employ_court_position_type = champion_court_position }
					}
					liege = scope:host
					can_be_employed_as = champion_court_position
				}
				trigger_else = {
					liege = scope:host
					can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:host }
				}
			}
		}
	}
	
	###################
	# ON ACTIONS
	###################

	pulse_actions = {
		entries = { guest_friendship guest_insulted tournament_guest_ptv_small tournament_acclaimed_participant tournament_entry_fee tournament_win_memory tournament_studied_techniques tournament_knight_skill spouse_skill_diplomacy_improves spouse_gives_hook relation_gains_friend host_gains_diplo_xp host_gains_steward_xp host_gains_intrigue_xp tournament_learned_from_knight tournament_game_of_rhetoric tournament_equipment_improved tournament_relaxed_in_tavern tournament_explored_village tournament_prayed_temple tournament_host_feted tournament_host_gifted tournament_tilting_demonstration tournament_archery_arrow_split tournament_recital_successful_verse tournament_joust_lance_broken tournament_joust_unhorsed tournament_horse_stumbles }
		chance_of_no_event = 3
	}

	on_start = { # Activity scope
		#if = { # Activity count debugging, left in for QA purposes
		#	limit = {
		#		debug_only = yes
		#	}
		#	if = {
		#		limit = {
		#			NOT = {
		#				exists = global_var:tournament_count
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_count
		#			value = 1
		#		}
		#	}
		#	else = {
		#		change_global_variable = {
		#			name = tournament_count
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier = tier_county }
		#			NOT = {
		#				exists = global_var:tournament_c
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_c
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier = tier_county }
		#		}
		#		change_global_variable = {
		#			name = tournament_c
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier = tier_duchy }
		#			NOT = {
		#				exists = global_var:tournament_d
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_d
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier = tier_duchy }
		#		}
		#		change_global_variable = {
		#			name = tournament_d
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier >= tier_kingdom }
		#			NOT = {
		#				exists = global_var:tournament_k
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_k
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host = { highest_held_title_tier >= tier_kingdom }
		#		}
		#		change_global_variable = {
		#			name = tournament_k
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_middle_east }
		#			NOT = {
		#				exists = global_var:tournament_world_middle_east
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_world_middle_east
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_middle_east }
		#		}
		#		change_global_variable = {
		#			name = tournament_world_middle_east
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_europe }
		#			NOT = {
		#				exists = global_var:tournament_world_europe
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_world_europe
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_europe }
		#		}
		#		change_global_variable = {
		#			name = tournament_world_europe
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_steppe }
		#			NOT = {
		#				exists = global_var:tournament_world_steppe
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_world_steppe
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_steppe }
		#		}
		#		change_global_variable = {
		#			name = tournament_world_steppe
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_africa }
		#			NOT = {
		#				exists = global_var:tournament_world_africa
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_world_africa
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_africa }
		#		}
		#		change_global_variable = {
		#			name = tournament_world_africa
		#			add = 1
		#		}
		#	}
		#	
		#	if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_india }
		#			NOT = {
		#				exists = global_var:tournament_world_india
		#			}
		#		}
		#		set_global_variable = {
		#			name = tournament_world_india
		#			value = 1
		#		}
		#	}
		#	else_if = {
		#		limit = {
		#			scope:host.location = { geographical_region = world_india }
		#		}
		#		change_global_variable = {
		#			name = tournament_world_india
		#			add = 1
		#		}
		#	}
		#}
		scope:host = {
			if = {
				limit = {
					has_character_flag = tournament_ai_override
				}
				remove_character_flag = tournament_ai_override
			}
		}
		if = {
			limit = { exists = scope:activity }
			### CHAMPION ###
			if = {
				limit = {
					exists = special_guest:special_guest_champion
					special_guest:special_guest_champion.liege = scope:host
					special_guest:special_guest_champion = {
						NOT = { has_court_position = champion_court_position }
					}
					scope:host = {
						can_appoint_char_to_court_position = {
							CHAR = scope:activity.special_guest:special_guest_champion
							COURT_POS = champion_court_position
						}
					}
				}
				scope:host = {
					send_interface_message = {
						type = event_champion_neutral
						title = tournament_activity_champion_appointed
						left_icon = scope:activity.special_guest:special_guest_champion
						court_position_grant_effect = {
							CANDIDATE = scope:activity.special_guest:special_guest_champion
							EMPLOYER = scope:host
							POS = champion
						}
					}
				}
			}
			if = { #Send a message and rewards
				limit = {
					exists = special_guest:special_guest_champion
					special_guest:special_guest_champion.liege = scope:host
				}
				special_guest:special_guest_champion = {
					save_scope_as = tournament_special_guest_champion
				}
				scope:host = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = tournament_personal_champion_tt
						left_icon = scope:host
						right_icon = scope:tournament_special_guest_champion

						scope:tournament_special_guest_champion = {
							add_opinion = {
						 		target = scope:host
						 		modifier = tournament_appointed_champion_opinion
						 		opinion = 35
							}

							if = { # If liege is a duke
								limit = {
									scope:host = {
										highest_held_title_tier = tier_duchy
									}
								}
								add_prestige = minor_prestige_value
							}
							else_if = { # If liege is a king
								limit = {
									scope:host = {
										highest_held_title_tier = tier_kingdom
									}
								}
								add_prestige = medium_prestige_value
							}
							else = { # If liege is an emperor
								add_prestige = major_prestige_value
							}
						}
					}
				}
			}
			# Opening event/invalidation if not enough people are present
			scope:host = {
				trigger_event = {
					on_action = tournament_opening_on_action
					days = 20
				}
			}
			
			scope:activity = {
				### PROGRESS TO VICTORY ###
				if = {
					limit = { activity_tournament_versus_contest_trigger = yes }
					set_variable = {
						name = progress_to_victory_threshold
						value = 10
					}
				}
				else = {
					set_variable = {
						name = progress_to_victory_threshold
						value = 20
					}
				}
				### LOCALES ###
				# Tavernkeeper
				if = {
					limit = { has_active_locale = tournament_locale_tavern }
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_tavernkeeper
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_tavernkeeper
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_tavernkeeper
						value = scope:tournament_tavernkeeper
					}
				}
				# Armorer
				if = {
					limit = {
						has_active_locale = tournament_locale_artisans
						OR = {
							has_current_phase = tournament_phase_melee
							has_current_phase = tournament_phase_joust
							has_current_phase = tournament_phase_duel
						}
					}
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_armorer
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_armorer
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_armorer
						value = scope:tournament_armorer
					}
				}
				# Tailor
				if = {
					limit = { has_active_locale = tournament_locale_artisans }
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_tailor
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_tailor
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_tailor
						value = scope:tournament_tailor
					}
				}
				# Priest
				if = {
					limit = { has_active_locale = tournament_locale_religious_building }
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_clergy = yes
								faith = scope:host.faith
								is_available_ai_adult = yes								
								has_no_particular_noble_roots_trigger = yes
								
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_clergy = yes
								faith = scope:host.faith
								is_available_ai_adult = yes
								has_no_particular_noble_roots_trigger = yes								
							}
							save_scope_as = tournament_priest
						}
					}
					else = {
						create_character = {
							template = tournament_priest_character_template
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_priest
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_priest
						value = scope:tournament_priest
					}
				}
				# Farrier
				if = {
					limit = {
						has_active_locale = tournament_locale_artisans
						OR = {
							has_current_phase = tournament_phase_horse_race
							has_current_phase = tournament_phase_joust
						}
					}
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_farrier
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_farrier
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_farrier
						value = scope:tournament_farrier
					}
				}
				# Bowyer
				if = {
					limit = {
						has_active_locale = tournament_locale_artisans
						has_current_phase = tournament_phase_archery
					}
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_bowyer
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_bowyer
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_bowyer
						value = scope:tournament_bowyer
					}
				}
				# Weaponsmith
				if = {
					limit = {
						has_active_locale = tournament_locale_artisans
						OR = {
							has_current_phase = tournament_phase_melee
							has_current_phase = tournament_phase_joust
							has_current_phase = tournament_phase_duel
						}
					}
					if = {
						limit = {
							any_pool_character = {
								province = scope:activity.activity_location
								is_available_ai_adult_locale_person = yes
							}
						}
						random_pool_character = {
							province = scope:activity.activity_location
							limit = {
								is_available_ai_adult_locale_person = yes
							}
							save_scope_as = tournament_weaponsmith
						}
					}
					else = {
						create_character = {
							template = tournament_locale_character
							dynasty = none
							location = scope:activity.activity_location
							save_scope_as = tournament_weaponsmith
						}
					}
					set_variable = { #we set a variable on the activity instead of the character in case another tournament grabs the same pool
						name = tournament_weaponsmith
						value = scope:tournament_weaponsmith
					}
				}
			}
		}
		
		# Make sure that Tournaments are held roughly every 5 years per major world area
		if = {
			limit = {
				scope:host.location = { geographical_region = world_middle_east }
				NOT = {
					exists = global_var:tournament_world_middle_east_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_middle_east_motivation
				value = yes
				days = 1825
			}
		}
		
		if = {
			limit = {
				scope:host.location = {
					geographical_region = world_europe
					NOT = {
						geographical_region = world_europe_west 
					}
				}
				NOT = {
					exists = global_var:tournament_world_europe_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_europe_motivation
				value = yes
				days = 1825
			}
		}
		
		if = {
			limit = {
				scope:host.location = { geographical_region = world_europe_west }
				NOT = {
					exists = global_var:tournament_world_europe_west_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_europe_west_motivation
				value = yes
				days = 1825
			}
		}
		
		if = {
			limit = {
				scope:host.location = { geographical_region = world_steppe }
				NOT = {
					exists = global_var:tournament_world_steppe_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_steppe_motivation
				value = yes
				days = 1825
			}
		}
		
		if = {
			limit = {
				scope:host.location = { geographical_region = world_africa }
				NOT = {
					exists = global_var:tournament_world_africa_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_africa_motivation
				value = yes
				days = 1825
			}
		}
		
		if = {
			limit = {
				scope:host.location = { geographical_region = world_india }
				NOT = {
					exists = global_var:tournament_world_india_motivation
				}
			}
			set_global_variable = {
				name = tournament_world_india_motivation
				value = yes
				days = 1825
			}
		}
	}

	on_enter_travel_state = { # Character scope
	}

	on_travel_state_pulse = { # Character scope
		trigger_event = { on_action = tournament_leave_travel_state }
	}

	on_leave_travel_state = {
		if = {
			limit = {
				NOT = { this = scope:host }
				is_playable_character = yes
				highest_held_title_tier >= tier_duchy
				primary_title = {
					is_mercenary_company = no
					is_holy_order = no
				}
			}
			save_scope_as = new_prestige_arrival
			scope:host = {
				add_to_variable_list = {
					name = new_prestige_arrival
					target = scope:new_prestige_arrival
				}
				if = {
					limit = {
						NOT = { has_character_flag = prestige_arrival_toast }
					}
					add_character_flag = prestige_arrival_toast
					trigger_event = {
						id = tournament_events.0200
						weeks = 2
					}
				}
			}
		}
		trigger_event = { on_action = tournament_leave_travel_state }
	}


	on_enter_passive_state = { # Character scope
		# Inform host of intent target arrival
		activity_intent_target_arrival_notification_effect = yes
		# Save as contestant or spectator based on eligibility
		save_scope_as = new_arrival
		if = {
			limit = {
				NAND = {
					this = scope:host
					scope:host.capital_province ?= scope:activity.activity_location
				}
				NOT = { has_character_flag = arrived_at_tournament_flag }
			}
			trigger_event = tournament_events.0050
		}
		# Add characters to contestant/spectator subset based on eligibility
		tournament_add_contestant_or_spectator_effect = yes
		if = {
			limit = { exists = current_travel_plan }
			current_travel_plan = {
				every_entourage_character = {
					limit = { is_participant_in_activity = scope:activity }
					save_scope_as = new_arrival
					tournament_add_contestant_or_spectator_effect = yes
				}
			}
		}
		# Remove score from previous contests (if relevant)
		if = {
			limit = { exists = var:progress_to_victory_score }
			remove_variable = progress_to_victory_score
		}
		# Set PtV threshold based on contest type
		if = {
			limit = { this = scope:host }
			scope:activity = {
				if = {
					limit = { activity_tournament_versus_contest_trigger = yes }
					set_variable = {
						name = progress_to_victory_threshold
						value = 10
					}
				}
				else = {
					set_variable = {
						name = progress_to_victory_threshold
						value = 20
					}
				}
			}
		}
		# Set/update PtV and aptitude
		set_passive_contest_aptitude_and_progress_effect = yes
		# Toasts for qualification
		if = {
			limit = {
				has_activity_state = passive
				is_ai = no
				exists = var:progress_to_victory
				NOT = {
					involved_activity = { has_current_phase = tournament_phase_ceremony }
				}
			}
			if = { # No Longer Qualify
				limit = { var:progress_to_victory < scope:activity.var:progress_to_victory_threshold }
				send_interface_toast = {
					type = toast_tournament_not_qualifying
					title = tournament_unqualified_ptv_start_toast
					left_icon = root
					custom_tooltip = tournament_unqualified_ptv_tt
				}
			}
			else_if = { # Now Qualify
				limit = { var:progress_to_victory >= scope:activity.var:progress_to_victory_threshold }
				send_interface_toast = {
					type = toast_tournament_qualifying
					title = tournament_qualified_ptv_toast
					left_icon = root
					custom_tooltip = tournament_qualified_ptv_tt 
				}
			}
		}
	}

	on_passive_state_pulse = { # Character scope
		# Fire host events
		if = {
			limit = { this = scope:host }
			trigger_event = { on_action = tournament_passive_state_host_pulse }
			# Set PtV threshold based on contest type
			scope:activity = {
				if = {
					limit = { activity_tournament_versus_contest_trigger = yes }
					set_variable = {
						name = progress_to_victory_threshold
						value = 10
					}
				}
				else = {
					set_variable = {
						name = progress_to_victory_threshold
						value = 20
					}
				}
			}
			chance_for_knight_appearance_effect = yes
		}
		# Fire guests events
		else = {
			trigger_event = { on_action = tournament_passive_state_guest_pulse }
		}
		# Set PtV threshold based on contest type
		set_passive_contest_aptitude_and_progress_effect = yes
	}

	on_enter_active_state = {
		surveyor_no_stone_unturned_perk_effect = yes
	}

	on_active_state_pulse = { # Character scope
		trigger_event = { on_action = tournament_active_state_pulse }
	}

	on_complete = { # Character scope
		# Remove remaining recurring characters.
		clean_up_recurring_characters_effect = yes
		tournament_clear_tournament_variables_effect = yes
		tournament_clear_tournament_modifiers_effect = yes
		# Dynasty Legacies
		host_has_taken_freebie_activity_effect = yes
		# Achievements.
		if = {
			# Limit it to the host so that we only do this _once_, even though they're not strictly relevant.
			limit = {
				this = scope:host
				exists = global_var:started_little_william_marshal_achievement # Overall tally
				exists = global_var:little_william_marshal_achievement_tally # Won tally
				global_var:started_little_william_marshal_achievement >= 3 # At least 3 contests held
				global_var:little_william_marshal_achievement_tally >= global_var:started_little_william_marshal_achievement # Won all of them
				# The variable was changed during this player's tournament and not during a player's tournament happening in parallel
				scope:activity = {
					any_attending_character = {
						is_ai = no
					}
				}
			}
			set_global_variable = {
				name = finished_little_william_marshal_achievement
				value = yes
			}
			set_global_variable = {
				name = little_william_marshal_achievement_tally
				value = 0
			}
			set_global_variable = {
				name = started_little_william_marshal_achievement
				value = 0
			}
		}
		else_if = {	# RESET THE COUNTER!
			limit = {
				this = scope:host
				exists = global_var:started_little_william_marshal_achievement # Overall tally
				exists = global_var:little_william_marshal_achievement_tally # Won tally
				# The variable was changed during this player's tournament and not during a player's tournament happening in parallel
				scope:activity = {
					any_attending_character = {
						is_ai = no
					}
				}
			}
			set_global_variable = {
				name = little_william_marshal_achievement_tally
				value = 0
			}
			set_global_variable = {
				name = started_little_william_marshal_achievement
				value = 0
			}
		}
	}

	###################
	# GRAPHICS
	###################

	### Plugin widgets
	activity_window_widgets = {
		tournament_toggle_my_knights_button = "guest_management_container"
		tournament_my_attending_knights_view = "activity_locale_other_custom_widgets"
		tournament_contest_information = "activity_locale_other_custom_widgets"

		tournament_progress_to_victory_widget = "activity_locale_window_right_bar_container"
		
		tournament_bottom_middle_widget = "activity_locale_bottom_middle"

		tournament_active_phase_contest_info_button = "bottom_right_content_before_guests"

		tournament_fullscreen_pivotal_event_widget = "widget_activity_locale_fullscreen_other_event"

		tournament_my_aptitudes = "activity_locale_bottom_left"

		tournament_active_phase_background = "activity_scene"
	}

	activity_planner_widgets = {
		tournament_contest_selection = "activity_plugin_center"
		tournament_planning_selected_contests = "planner_details_header"
	}
}
